<template>
  <el-row class="dc-container" v-loading="loading">
    <history ref="history" :bussObject="curentRow"></history>
    <edit-form
      ref="editForm"
      v-on:save-finished="getList"
      v-on:before-load="setLoad"
      v-on:after-load="resetLoad"
      :permission="permission"
    ></edit-form>
    <info-form
      ref="infoForm"
      v-on:save-finished="getList"
      v-on:before-load="setLoad"
      v-on:after-load="resetLoad"
      :permission="permission"
    ></info-form>
<!--    <el-row gutter="0" type="flex" justify="start" align="top" class="dc-el-row_ElRow">-->
<!--      <el-col :span="6">-->
<!--        -->
<!--      </el-col>-->
<!--      <el-col :span="6">-->
<!--        <div style="text-align: right">-->
<!--          <el-button-group class="dc-el-button-group_ButtonGroup">-->
<!--            <el-tooltip effect="dark" content="搜索" placement="top">-->
<!--              <el-button v-on:click="onSearch" type="primary" icon="el-icon-search"></el-button>-->
<!--            </el-tooltip>-->
<!--            <el-tooltip effect="dark" content="重置" placement="top">-->
<!--              <el-button-->
<!--                v-on:click="isQueryConditionPanel ? $refs.conditionPanel.reset() : $refs.queryForm.resetFields()"-->
<!--                type="primary"-->
<!--                icon="el-icon-refresh-right"-->
<!--              ></el-button>-->
<!--            </el-tooltip>-->
<!--            <el-tooltip effect="dark" content="更多" placement="top">-->
<!--              <el-button v-on:click="onQueryConditionPanel('query')" type="primary" icon="el-icon-d-arrow-right"></el-button>-->
<!--            </el-tooltip>-->
<!--          </el-button-group>-->
<!--        </div>-->
<!--      </el-col>-->
<!--    </el-row>-->
    <el-form
      ref="queryForm"
      :model="queryFormData"
      label-width="100px"
      v-if="!isQueryConditionPanel"
      class="dc-el-form_ElQueryForm"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item prop="" label="项目编号：" class="dc-el-form-item_SingleInput">
            <el-input
              v-model="queryFormData.projectCode"
              placeholder="请输入"
              clearable
              class="dc-el-input_SingleInput"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="" label="项目名称：" class="dc-el-form-item_SingleInput">
            <el-input
              v-model="queryFormData.projectName"
              placeholder="请输入"
              clearable
              class="dc-el-input_SingleInput"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="" label="项目经理：" class="dc-el-form-item_SelectInput">
            <el-input
              v-model="queryFormData.projectManager"
              placeholder="请输入"
              clearable
              class="dc-el-input_SingleInput"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6" class="dc-el-form-item_SelectInput">
          <el-tooltip disabled class="dc-el-tooltip_Button">
            <el-button @click="getList" style="margin-left:20px" icon="el-icon-search"
                       class="dc-el-button_Button" type="primary">查询</el-button>
          </el-tooltip>
          <el-tooltip disabled class="dc-el-tooltip_Button">
            <el-button icon="el-icon-refresh" class="dc-el-button_Button" @click="handleReset">重置</el-button>
          </el-tooltip>
        </el-col>
        <el-col :span="24"></el-col>
      </el-row>
    </el-form>
    <el-row gutter="0" type="flex" justify="start" align="top" class="dc-el-row_ElRow">
      <el-tooltip v-show="permission.add" manual="true" class="dc-el-tooltip_Button">
        <el-button type="primary" icon="el-icon-plus" v-on:click="onCreate">新增</el-button>
      </el-tooltip>
    </el-row>
    <ux-grid
      column-key
      ref="table"
      :data="tableData"
      border
      @selection-change="handleRowClick"
      v-on:sort-change="onSortChange"
      v-on:header-dragend="onWidthChange($refs.table)"
      :tree-config="{ children: '', indent: 20, accordion: false, line: false, showIcon: true, iconOpen: '', iconClose: '' }"
      class="dc-ux-grid_QueryTable"
    >
      <ux-table-column
        :params="{ sortId: '160', summary: false }"
        type="checkbox"
        tree-node
        resizable
        min-width="80px"
        fixed="left"
        align="center"
        header-align="center"
        class="dc-ux-table-column_ElTableCheckBoxColumn"
      ></ux-table-column>
      <ux-table-column
        title="序号"
        :params="{ sortId: '161', summary: false }"
        type="index"
        tree-node
        resizable
        min-width="80px"
        fixed="left"
        align="center"
        header-align="center"
        class="dc-ux-table-column_ElTableNumColumn"
      ></ux-table-column>
      <ux-table-column
        :field="tableColumns['129'].field"
        :title="tableColumns['129'].title"
        :width="tableColumns['129'].width"
        :visible="tableColumns['129'].visible"
        :params="{ sortId: '129', summary: false }"
        tree-node
        resizable
        min-width="120px"
        align="left"
        header-align="center"
        show-overflow
        class="dc-ux-table-column_ElTableColumn"
      ></ux-table-column>
      <ux-table-column
        :field="tableColumns['130'].field"
        :title="tableColumns['130'].title"
        :width="tableColumns['130'].width"
        :visible="tableColumns['130'].visible"
        :params="{ sortId: '130', summary: false }"
        tree-node
        resizable
        min-width="120px"
        align="left"
        header-align="center"
        show-overflow
        class="dc-ux-table-column_ElTableColumn"
      >
        <template slot-scope="{ row }">
          <span style="cursor: pointer; color: #018CB7" @click="handleInfo(row)">{{ row.projectCode }}</span>
        </template>
      </ux-table-column>
      <ux-table-column
        :field="tableColumns['131'].field"
        :title="tableColumns['131'].title"
        :width="tableColumns['131'].width"
        :visible="tableColumns['131'].visible"
        :params="{ sortId: '131', summary: false }"
        tree-node
        resizable
        min-width="120px"
        align="left"
        header-align="center"
        show-overflow
        class="dc-ux-table-column_ElTableColumn"
      ></ux-table-column>
      <ux-table-column
        :field="tableColumns['132'].field"
        :title="tableColumns['132'].title"
        :width="tableColumns['132'].width"
        :visible="tableColumns['132'].visible"
        :params="{ sortId: '132', summary: false }"
        tree-node
        resizable
        min-width="120px"
        align="left"
        header-align="center"
        show-overflow
        class="dc-ux-table-column_ElTableColumn"
      ></ux-table-column>
      <ux-table-column
        :field="tableColumns['133'].field"
        :title="tableColumns['133'].title"
        :width="tableColumns['133'].width"
        :visible="tableColumns['133'].visible"
        :params="{ sortId: '133', summary: false }"
        tree-node
        resizable
        min-width="120px"
        align="left"
        header-align="center"
        show-overflow
        class="dc-ux-table-column_ElTableColumn"
      ></ux-table-column>
      <ux-table-column
        :field="tableColumns['134'].field"
        :title="tableColumns['134'].title"
        :width="tableColumns['134'].width"
        :visible="tableColumns['134'].visible"
        :params="{ sortId: '134', summary: false }"
        tree-node
        resizable
        min-width="120px"
        align="left"
        header-align="center"
        show-overflow
        class="dc-ux-table-column_ElTableColumn"
      ></ux-table-column>
      <ux-table-column
        :field="tableColumns['135'].field"
        :title="tableColumns['135'].title"
        :width="tableColumns['135'].width"
        :visible="tableColumns['135'].visible"
        :params="{ sortId: '135', summary: false }"
        tree-node
        resizable
        min-width="120px"
        align="left"
        header-align="center"
        show-overflow
        class="dc-ux-table-column_ElTableColumn"
      ></ux-table-column>
      <ux-table-column
        :field="tableColumns['136'].field"
        :title="tableColumns['136'].title"
        :width="tableColumns['136'].width"
        :visible="tableColumns['136'].visible"
        :params="{ sortId: '136', summary: false }"
        tree-node
        resizable
        min-width="120px"
        align="left"
        header-align="center"
        show-overflow
        class="dc-ux-table-column_ElTableColumn"
      ></ux-table-column>
    </ux-grid>
    <el-pagination
      :total="tableDataTotal"
      :page-size="search.limit"
      background
      :current-page="tableDataPage"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      v-on:size-change="onSizeChange"
      v-on:current-change="onCurrentChange"
      class="dc-el-pagination_ElPagination"
    ></el-pagination>
  </el-row>
</template>
<script>
import { validatenull } from '@/utils/validate'

import { listProjectPage, deleteTeamMember } from '@/api/team/teamMember'

import { listResourcePermission } from '@/api/admin/common/permission'

/** 根据用户界面配置import组件 开始 */
import History from '@/views/components/history'
import EditForm from './form'
import InfoForm from './info'
import QueryTag from '@/views/components/queryTag'
import QueryConditionPanel from '@/views/components/queryConditionPanel'
import ViewColumnsSelect from '@/views/components/DcViewColumnsSelect'
/** 根据用户界面配置import组件 结束 */
import metadata from './metadata'
import ExportExcelButton from '@/components/ExportExcelButton'
import DcMain from '@/views/components/dcMain'
import OperationIcon from '@/components/OperationIcon'
export default {
  extends: DcMain,
  components: {
    /** 根据用户界面配置组件 开始 */
    History,
    InfoForm,
    EditForm,
    QueryTag,
    QueryConditionPanel,
    ViewColumnsSelect,
    /** 根据用户界面配置组件 结束 */
    ExportExcelButton,
    OperationIcon
  },
  data() {
    return {
      selections: [],
      permission: {
        view: false,
        add: false,
        edit: false,
        remove: false,
        export: false
      },
      metadata,

      /** 根据用户界面配置生成data数据 开始 */
      queryFormData: {},
      tableData: [
        {projectCode: '18485236985'}
      ],
      // 查询表格列头
      tableColumns: {
        160: {
          isShow: true,
          visible: true,
          order: 0
        },
        161: {
          title: '序号',
          isShow: true,
          visible: true,
          order: 1
        },
        129: {
          title: '项目名称',
          field: 'projectName',
          visible: true,
          order: 2
        },
        130: {
          title: '项目编号',
          field: 'projectCode',
          visible: true,
          order: 3
        },
        131: {
          title: '项目简称',
          field: 'projectShortName',
          visible: true,
          order: 4
        },
        132: {
          title: '项目状态',
          field: 'projectStatus.name',
          visible: true,
          order: 5
        },
        133: {
          title: '项目经理',
          field: 'projectManager.name',
          visible: true,
          order: 6
        },
        134: {
          title: '成员数量',
          field: 'memberCount',
          visible: true,
          order: 7
        },
        135: {
          title: '项目计划开始日期',
          field: 'planBeginDate',
          visible: true,
          order: 8
        },
        136: {
          title: '项目计划结束日期',
          field: 'planEndDate',
          visible: true,
          order: 9
        }
      },
      // 分页属性
      tableDataPage: 1,
      tableDataTotal: 0,
      // 选项变量

      /** 根据用户界面配置生成data数据 结束 */
      version: 3,
      search: {
        // 查询条件   业务表设置的筛选条件
        params: [],

        offset: 0,
        limit: 10,
        columnName: '', // 排序字段名
        order: '' // 排序
      },
      searchList: [],
      tableId: '1740772218121938036',
      schemeId: '1740772218121938034'
    }
  },
  computed: {},
  methods: {
    handleRowClick (row) {
      this.selections = row;
    },
    handleInfo (row) {
      this.$refs.infoForm.$emit('openViewDialog', row)
    },
    handleReset () {
      this.queryFormData = {};
      this.getList();
    },
    getList() {
      this.setLoad()
      /* 查询参数 和数据权限 */
      this.search.params = []
      if (this.queryFormData.projectCode) {
        this.search.params.push({
          "columnName": "project_code",
          "queryType": "like",
          "value": `%${this.queryFormData.projectCode}%`
        },)
      }
      if (this.queryFormData.projectName) {
        this.search.params.push({
          "columnName": "project_name",
          "queryType": "like",
          "value": `%${this.queryFormData.projectName}%`
        },)
      }
      if (this.queryFormData.projectManager) {
        this.search.params.push({
          "columnName": "projectManager.name",
          "queryType": "like",
          "value": `%${this.queryFormData.projectManager}%`
        },)
      }
      // 数据权限: 团队成员信息team_member
      this.pushDataPermissions(this.search.params, this.$route.meta.routerId, this.tableId)
      listProjectPage(this.search)
        .then((responseData) => {
          if (responseData.code == 100) {
            this.tableDataTotal = responseData.data.total
            this.tableData = responseData.data.rows ? responseData.data.rows : []
          } else {
            this.showMessage(responseData)
          }
          this.resetLoad()
        })
        .catch((error) => {
          this.outputError(error)
        })
    },
    onSearch() {
      if (this.isQueryConditionPanel) {
        this.search.offset = 0
        this.tableDataPage = 1
        this.getList()
      } else {
        this.$refs['queryForm'].validate((valid) => {
          if (valid) {
            this.search.offset = 0
            this.tableDataPage = 1
            this.getList()
          } else {
            return false
          }
        })
      }
    },
    onSizeChange(val) {
      this.tableDataPage = 1
      this.search.limit = val
      this.search.offset = (this.tableDataPage - 1) * val
      this.getList()
    },
    onCurrentChange(val) {
      this.search.offset = (val - 1) * this.search.limit
      this.tableDataPage = val
      this.getList()
    },
    async pageInit() {
      this.setLoad()
      this.initOptions(this.queryModel)
      this.onSearch()
      listResourcePermission(this.$route.meta.routerId)
        .then((responseData) => {
          if (responseData.code == 100) {
            this.permission.view = responseData.data.find((item) => {
              return item.permission === 'teamMember:read'
            })
            this.permission.export = responseData.data.find((item) => {
              return item.permission === 'teamMember:export'
            })

            this.permission.add = responseData.data.find((item) => {
              return item.permission === 'teamMember:add'
            })
            this.permission.edit = responseData.data.find((item) => {
              return item.permission === 'teamMember:update'
            })
            this.permission.remove = responseData.data.find((item) => {
              return item.permission === 'teamMember:delete'
            })
          } else {
            this.showMessage(responseData)
          }
          this.resetLoad()
        })
        .catch((error) => {
          this.outputError(error)
        })
    },
    onView(scope) {
      this.$refs.editForm.$emit('openViewDialog', scope.row.id)
    },

    onCreate() {
      if (!this.selections.length) {
        this.$message({
          type: 'warning',
          message: '请先选择项目'
        });
        return;
      }
      if (this.selections.length > 1) {
        this.$message({
          type: 'warning',
          message: '请选择单条数据'
        });
        return;
      }
      this.$refs.editForm.$emit('openAddDialog', this.selections[0])
    },
    onEdit(scope) {
      this.$refs.editForm.$emit('openEditDialog', scope.row.id)
    },
    onCopy(scope) {
      this.$refs.editForm.$emit('openCopyDialog', scope.row.id)
    },
    onDelete(scope) {
      this.$confirm('确定删除吗？', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.setLoad()
          deleteTeamMember(scope.row)
            .then((responseData) => {
              if (responseData.code == 100) {
                this.getList()
                this.showMessage({
                  type: 'success',
                  msg: '删除成功'
                })
              } else {
                this.showMessage(responseData)
              }
              this.resetLoad()
            })
            .catch((error) => {
              this.outputError(error)
            })
        })
        .catch(() => {})
    },

    onSortChange(orderby) {
      if (validatenull(orderby.prop)) {
        this.search.columnName = ''
        this.search.order = ''
      } else {
        this.search.columnName = orderby.prop
        this.search.order = orderby.order
      }
      this.getList()
    },

    // 初始化自定义类型选择框选项变量
    initOptions(This) {}
  },
  watch: {},
  mounted() {
    this.pageInit()

    this.columnDrop(this.$refs.table)
  }
}
</script>
